<?php

try{
	require('connect.php');
	function getIPLoc_QQ($queryIP){ 
	    $url = 'http://ip.qq.com/cgi-bin/searchip?searchip1='.$queryIP; 
	    $ch = curl_init($url); 
	    curl_setopt($ch,CURLOPT_ENCODING ,'gb2312'); 
	    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
	    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true) ; // 获取数据返回 
	    $result = curl_exec($ch); 
	    $result = mb_convert_encoding($result, "utf-8", "gb2312"); // 编码转换，否则乱码 
	    curl_close($ch); 
	    preg_match("@<span>(.*)</span></p>@iU",$result,$ipArray); 
	    $loc = $ipArray[1]; 
	    return $loc; 
	}
	$pdo->query("set names utf8");
	if(isset($_POST['wall_first']) && isset($_POST['wall_sec']) && isset($_POST['wall_author']))
	{
		$wall_ip = $_SERVER['REMOTE_ADDR'];
		$wall_first = $_POST['wall_first'];
		$wall_sec = $_POST['wall_sec'];
		$wall_author = $_POST['wall_author'];
		$wall_addtime = date("Y-m-d_h:i:s");
		$wall_loc = getIPLoc_QQ($wall_ip);
		$query = "insert into wall(wall_first,wall_sec,wall_author,wall_addtime,wall_ip,wall_loc) values(?,?,?,?,?,?)";
		$result = $pdo -> prepare($query);
		$result -> bindParam(1,$wall_first);
		$result -> bindParam(2,$wall_sec);
		$result -> bindParam(3,$wall_author);
		$result -> bindParam(4,$wall_addtime);
		$result -> bindParam(5,$wall_ip);
		$result -> bindParam(6,$wall_loc);
		if($result -> execute())
			header("Location:success.html");
		else
			header("Location:error.html");
	}
}catch(PDOException $e){
	echo"连接数据库失败！".$e->getMessage();
}

?>
<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<script type="text/javascript" src="../js/jquery.min.js"></script>
		<link rel="stylesheet" href="../css/bootstrap.min.css">
		<link rel="icon" href="../img/ico_logo.ico" type="image/gif" sizes="16x16" />
		<script src="../js/bootstrap.min.js"></script>
		<link rel="stylesheet" href="../css/mycss_s1.css">
		<script type="text/javascript"src="../js/myjs.js"></script>
		<script type="text/javascript"src="../js/jquery-ui-1.10.4.custom.js"></script>
		<title>VGEE.cn</title>

	</head>
	<body id="form" style="background:#272822">
		<div style="display:none" class="change"></div>
		<div class="container" >
				<div id="block" class="form-group">
					<form name="loginForm" action="query.php" method="POST">
					<table>
						<tr>
							<th></th>
							<th>添加段落</th>
						</tr>
						<tr>
							<td>第一句</td>
							<td><input type="text" placeholder="输入第一句话" name="wall_first" id="wall_first" class="form-control"></td>
						</tr>
						<tr>
							<td>第二句</td>
							<td><input type="text" placeholder="输入第二句话" name="wall_sec" id="wall_sec" class="form-control"></td>
						</tr>
						<tr>
							<td>作者</td>
							<td><input type="text" placeholder="输入作者" name="wall_author" id="wall_author" class="form-control"></td>
						</tr>
						<tr>
							<td></td>
							<td><button id="submit" type="submit" name="submit" class="btn btn-success">提交</button></td>
						</tr>
					</table>
					</form>
				</div>
		</div>
	</body>